<template>
    <div id="monthWorkList">
      <el-dialog title="角色信息" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item label="角色名称" prop="rName" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input v-model="form.rName" autocomplete="off"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="角色类型" prop="rType" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-select style="width: 100%;" v-model="form.rType" clearable placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="备注" prop="rNode" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input v-model="form.rNode" autocomplete="off"></el-input>
            </el-col>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="formSubmit('form')">确 定</el-button>
        </div>
      </el-dialog>
      <el-dialog title="分配权限" :visible.sync="dialogAllotVisible">
        <el-form>
          <el-form-item label="选择权限" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-select v-if="roleTypeId" multiple v-model="valueRole" placeholder="请选择权限">
                <el-option
                  v-for="item in roleList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              <el-select v-else multiple :multiple-limit="one"  v-model="valueRole" placeholder="请选择权限">
                <el-option
                  v-for="item in roleList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-col>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogAllotVisible = false">取 消</el-button>
          <el-button type="primary" @click="allotSubmit">确 定</el-button>
        </div>
      </el-dialog>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="角色名称">
          <el-input v-model="formInline.name" placeholder="角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色类型">
          <el-select v-model="formInline.type" clearable placeholder="请选择"  @change="getTableList">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="success" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            prop="rName"
            label="角色名称"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            prop="rTypeName"
            label="角色类型"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            prop="rStatusName"
            label="状态"
            align="center">
          </el-table-column>
          <el-table-column
            prop="rNode"
            label="备注"
            align="center">
          </el-table-column>
          <el-table-column
            prop="rCreateTime"
            label="创建时间"
            align="center">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="300">
            <template slot-scope="scope">
              <el-button @click="handleClick(scope.row)" type="text" size="small">修改</el-button>
              <el-button @click.native.prevent="deleteRow(scope.$index, tableData)" type="text" size="small">删除</el-button>
              <el-button @click="handleAllot(scope.row)" type="text" size="small">分配权限</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[15, 25, 50]"
            :page-size="perPage"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
        <el-input v-model="rowId" type="hidden"></el-input>
    </div>
</template>
<script>
export default {
  name: 'monthWorkList',
  data () {
    return {
      formInline: {
        name: '',
        type: ''
      },
      form: {
        rName: '',
        rType: '',
        rNode: ''
      },
      rules: {
        rName: [
          { required: true, message: '请填写角色名称', trigger: 'blur' }
        ],
        rType: [
          { required: true, message: '请选择角色类型', trigger: 'change' }
        ]
        // ,
        // rNode: [
        //   { required: true, message: '请填写备注', trigger: 'blur' }
        // ]
      },
      one:1,
      valueRole:[],
      valueRoles:'',
      roleList:[],
      operaType:'',
      rowId:'',
      roleTypeId:false,
      formLabelWidth: '320px',
      dialogFormVisible:false,
      dialogAllotVisible:false,
      currentPage: 1,
      perPage: 15,
      total: null,
      options: [{
          value: '1',
          label: '菜单角色'
        }, {
          value: '2',
          label: '数据角色'
        }, {
          value: '3',
          label: '操作角色'
        }],
      tableData: []
    };
  },
  created() {
    //调用加载表格方法
    this.getTableList();
  },
  beforeDestroy() {},
  computed: {},
  mounted() {},
  methods: {
    onSubmit() {
      console.log('submit!');
      this.getTableList();
    },
    //新增
    add() {
      var self = this;
      self.dialogFormVisible = true;
      self.operaType = 'add';
      self.form.rName = '';
      self.form.rType = '';
      self.form.rNode = '';
    },
    //修改
    handleClick(row) {
      console.log(row);
      var self = this;
      self.rowId = row.id;
      self.operaType = 'update';
      self.dialogFormVisible = true;
      self.form.rName = row.rName;
      self.form.rType = row.rType;
      self.form.rNode = row.rNode;
      
    },
    //确定新增项目
    formSubmit(formName) {
      //console.log('submit!');    
      this.$refs[formName].validate((valid) => {
          if (valid) {
            var self = this;
            console.log(self.operaType)
            if(self.operaType == 'add'){
              self.
                postRequest("/role/insert", {
                  rName:self.form.rName,
                  rType:self.form.rType,
                  rNode:self.form.rNode
                }).then(
                  res => {      
                    console.log(res) 
                    if(res.data.result == '1' ){            
                      this.$message({
                          message: res.data.message,
                          type: 'success'
                      });
                    }else if(res.data.result == '2' ){
                      this.$message({
                          message: res.data.message,
                          type: 'error'
                      });
                    }else if(res.data.result == '-2' ){
                      this.$message({
                          message: res.data.message,
                          type: 'error'
                      });
                    }    
                    this.dialogFormVisible = false;
                    self.getTableList();
                  }
                );
            }else if(self.operaType == 'update'){
              //console.log(self.rowId)
              self.
                postRequest("/role/update", {
                  rName:self.form.rName,
                  rType:self.form.rType,
                  rNode:self.form.rNode,
                  id:self.rowId
                }).then(
                  res => {      
                    //console.log(res) 
                    if(res.data.result == '1' ){            
                      this.$message({
                          message: res.data.message,
                          type: 'success'
                      });
                    }else if(res.data.result == '2' ){
                      this.$message({
                          message: res.data.message,
                          type: 'error'
                      });
                    }else if(res.data.result == '-2' ){
                      this.$message({
                          message: res.data.message,
                          type: 'error'
                      });
                    }else if(res.data.result == '-10'){
                      this.$message({
                          message: res.data.message,
                          type: 'info'
                      });
                    }    
                    this.dialogFormVisible = false;
                    self.getTableList();
                    self.codeBtype = false;
                  }
                );
            }
          }
      })  
      
      
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      var self = this;
      self.perPage = val;
      self.getTableList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      var self = this;
      self.currentPage = val;
      self.getTableList();
    },
    deleteRow(index, rows) {
      console.log(rows[index])
      var self = this;
      self.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          self.
            postRequest("/role/delete", {
              id:rows[index].id
            }).then(
              res => {      
                console.log(res.data) 
                if(res.data.result == '1' ){            
                  this.$message({
                      message: res.data.message,
                      type: 'success'
                  });
                }else if(res.data.result == '2' ){
                  this.$message({
                      message: res.data.message,
                      type: 'error'
                  });
                }else if(res.data.result == '-10'){
                  this.$message({
                      message: res.data.message,
                      type: 'info'
                  });
                }    
                self.getTableList();
              }
            );
        }).catch(() => {
          self.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //表格加载
    getTableList() {
      var self = this;
      //console.log(self.currentPage)
      self.getRequest("/role/queryByPage?page_no=" +self.currentPage+'&&page_size='+self.perPage
       +'&&name='+self.formInline.name+'&&type='+self.formInline.type
      , {}).then(
        res => {      
          console.log(res.data)     
          self.tableData = res.data.data;
          self.total = res.data.recordsTotal;
          self.perPage = res.data.length;
        }
      );
    },
    handleAllot(row){ 
      var self = this;
      self.dialogAllotVisible = true;
      self.rowId = row.id;
      if(row.rType == '2'){
        self.roleTypeId = false;
      }else if(row.rType == '1' || row.rType == '3'){
        self.roleTypeId = true;
      }
      //查询权限
      self.
        postRequest("/role/isAllotPermissions", {
          id:self.rowId,
          pType:row.rType
        }).then(
          res => {      
            console.log(res.data.allPermissions) 
            var obj = res.data.allPermissions;
            var objHas = res.data.hasPermissions;
            var arr = [];
            var arrSelect = [];
            for(var i in objHas){
              arrSelect.push(objHas[i].id);
              //console.log(objHas[i].id)
            }
            //console.log(self.valueRole)
            for(var i in obj) {
              var row = {};
              row.label = obj[i].name;
              row.value = obj[i].id;
              arr.push(row);
            }
            //console.log(arr)
            self.roleList = arr;
            self.valueRole = arrSelect;

          }
        );
    },
    allotSubmit() {
      var self = this;
      var valueRoleList = [];
      
      
      if(self.valueRoles != ''){
        valueRoleList.push(self.valueRoles);
      }else{
        valueRoleList.push(self.valueRole);
      }

      if(valueRoleList == null || valueRoleList == ''){
        this.$message({
            message: '提交不能为空',
            type: 'error'
        });
        return false;
      }
      //console.log(valueRoleList)
      self.
        postRequest("/role/allotPermission", {
          id:self.rowId,
          permissionIds:valueRoleList
        }).then(
          res => {      
            //console.log(res) 
            if(res.data.result == '1' ){            
              this.$message({
                  message: res.data.message,
                  type: 'success'
              });
            }else if(res.data.result == '2' ){
              this.$message({
                  message: res.data.message,
                  type: 'error'
              });
            }else if(res.data.result == '-10'){
              this.$message({
                  message: res.data.message,
                  type: 'info'
              });
            }   
            this.dialogAllotVisible = false;
            self.getTableList();
          }
        );
    },
  }
}
</script>

<style scoped>
  .demo-form-inline {text-align: left;}
  .el-pagination {margin: 20px 0;}
</style>